<?php

namespace addons\yungift\controller;

use addons\yungift\library\Logic;
use addons\yungift\library\Super;
use addons\yungift\model\FundRecord;
use addons\yungift\model\FundUser;

/**
 * 礼币接口
 */
class Fund extends BaseApi
{
    // 获取我的礼币
    public function getMineData()
    {
        if (! $fundUser = FundUser::get(['activity_id' => $this->activityId, 'member_id' => $this->memberId])) {
            $this->error('礼币账户不存在');
        }
        $fundInfo = Super::arrayGetSet($fundUser, [
            'amount_left',
            'status'
        ]);

        $this->success('success', $fundInfo);
    }

    // 获取礼品记录
    public function getRecordList()
    {
        if (! $fundUser = FundUser::get(['activity_id' => $this->activityId, 'member_id' => $this->memberId])) {
            $this->error('礼币账户不存在');
        }

        // 数据列表
        $query = FundRecord::where(['fund_user_id' => $fundUser->id])
            ->order('id DESC');
        $result = $query->paginate($this->request->post('page_size', 10), false);
        $result = Logic::getPageData($result, [
            'id',
            'amount',
            'after_amount',
            'action',
            'action_text' => 'actionText',
            'created_at',
        ]);

        $this->success('success', $result);
    }
}
